﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using E2_WEBSITE.Code.Product;

namespace E2_WEBSITE.cp.employee.manage
{
    public partial class ProductDetail : System.Web.UI.Page
    {
        private ProductController productController = null;

        private void FillLoaiSanPhamDropList()
        {
            dlLoaiSanPham.Items.Clear();

            if (productController != null)
            {
                List<LoaiSanPham> lspList = productController.GetLoaiSanPhamList();
                if (lspList != null)
                {
                    for (int i = 0; i < lspList.Count; i++)
                    {
                        dlLoaiSanPham.Items.Add(lspList[i].TenLoaiSanPham);
                    }
                }
            }
        }

        private void FillNhaSanXuatDropList()
        {
            dlNhaSanXuat.Items.Clear();
            if (productController != null)
            {
                List<NhaSanXuat> nsxList = productController.GetNhaSanXuatList();
                if (nsxList != null)
                {
                    for (int i = 0; i < nsxList.Count; i++)
                    {
                        dlNhaSanXuat.Items.Add(nsxList[i].TenNSX);
                    }
                }
            }
        }

        private void FillNhaPhanPhoiDropList()
        {
            dlNhaPhanPhoi.Items.Clear();

            if (productController != null)
            {
                List<NhaPhanPhoi> nppList = productController.GetNhaPhanPhoiList();
                if (nppList != null)
                {
                    for (int i = 0; i < nppList.Count; i++)
                    {
                        dlNhaPhanPhoi.Items.Add(nppList[i].TenNhanPhanPhoi);
                    }
                }
            }
        }

        private void FillKhuyenMaiDropList()
        {
            dlKhuyenMai.Items.Clear();

            if (productController != null)
            {
                List<KhuyenMai> kmList = productController.GetKhuyenMaiList();
                if (kmList != null)
                {
                    for (int i = 0; i < kmList.Count; i++)
                    {
                        if (kmList[i].PhanTramGiam != 0)
                            dlKhuyenMai.Items.Add(kmList[i].HinhThuc.Trim() + " " + kmList[i].PhanTramGiam.ToString() + "%");
                        else
                            dlKhuyenMai.Items.Add(kmList[i].HinhThuc.Trim());
                    }
                    dlKhuyenMai.Items.Add("Không có");
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            productController = new ProductController();
            if (!IsPostBack)
            {
                dlLoaiSuDung.Items.Add("Desktop");
                dlLoaiSuDung.Items.Add("Laptop");
                dlLoaiSuDung.Items.Add("Desktop và Laptop");

                String strID = Request.QueryString["ID"];
                if (strID == "")
                    return;
                
                int id;
                try
                {
                    id = Convert.ToInt32(strID);
                }
                catch
                {
                    return;
                }

                FillLoaiSanPhamDropList();
                FillNhaSanXuatDropList();
                FillNhaPhanPhoiDropList();
                FillKhuyenMaiDropList();

                ProductInfo productInfo = null;
                productInfo = productController.getProductById(id);

                if (productInfo != null)
                {
                    ImgProduct.ImageUrl = "~/images/HinhAnhSanPham/" + productInfo.Hinh_Anh.URL;
                    dlLoaiSanPham.Text = productInfo.Loai_San_Pham.TenLoaiSanPham;
                    txtMaSanPham.Text = productInfo.ID.ToString();
                    txtTenSanPham.Text = productInfo.TenSanPham;
                    txtMoTa.Text = productInfo.MoTa;
                    dlNhaSanXuat.Text = productInfo.NSX.TenNSX;
                    dlNhaPhanPhoi.Text = productInfo.Nha_Phan_Phoi.TenNhanPhanPhoi;
                    txtGia.Text = productInfo.Gia.ToString();
                    
                    if (productInfo.Khuyen_Mai.HinhThuc != null)
                        dlKhuyenMai.Text = productInfo.Khuyen_Mai.HinhThuc;
                    else
                        dlKhuyenMai.Text = "Không có";

                    txtDiem.Text = productInfo.Diem.ToString();

                    txtBaoHanh.Text = productInfo.BaoHanh;

                    txtSoLuong.Text = productInfo.SoLuongTon.ToString();

                    txtNgayThemSanPham.Text = productInfo.NgayThemSP.ToString();

                    switch (productInfo.LoaiLinhKien)
                    {
                        case 0:
                            dlLoaiSuDung.Text = "Desktop";
                            break;
                        case 1:
                            dlLoaiSuDung.Text = "Laptop";
                            break;
                        case 2:
                            dlLoaiSuDung.Text = "Desktop và Laptop";
                            break;
                    }
                }
            }
        }

        protected void btnHuyDonSanPham_Click(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(txtMaSanPham.Text);
            if (productController.DeleteProductWithID(id))
            {
                WebMsgBox.Show("Đã xóa sản phẩm!");
                ClientScript.RegisterStartupScript(this.GetType(), "Delay then redirect", "<script type=text/javascript>delayRedirect('/cp/employee/manage/ProductManage.aspx');</script>");
            }
            else
                WebMsgBox.Show("Thất bại khi xóa sản phẩm!");
        }

        private NhaSanXuat GetNSX(String strTenNhaXuat)
        {
            List<NhaSanXuat> nsxList = productController.GetNhaSanXuatList();
            for (int i = 0; i < nsxList.Count; i++)
            {
                if (nsxList[i].TenNSX == strTenNhaXuat)
                    return nsxList[i]; 
            }
            return null;
        }

        private KhuyenMai GetKhuyenMai(String strKhuyenMai)
        {
            int length = strKhuyenMai.Length;
            int i;
            for (i = length - 1; i > 0; i--)
            {
                if (strKhuyenMai[i] == ' ')
                    break;
            }

            String strPhanTramGiam = strKhuyenMai.Substring(i+1,length-i-2);
            String strHinhThuc = strKhuyenMai.Substring(0,i);

            List<KhuyenMai> kmList = productController.GetKhuyenMaiList();
            for (int j = 0; j < kmList.Count; j++)
            {
                if ((kmList[j].HinhThuc.Trim() == strHinhThuc) && (kmList[j].PhanTramGiam.ToString() == strPhanTramGiam))
                {
                    return kmList[j];
                }
            }
            return null;
        }

        private NhaPhanPhoi GetNhaPhanPhoi(String strNhaPhanPhoi)
        {
            List<NhaPhanPhoi> nppList = productController.GetNhaPhanPhoiList();
            for (int i = 0; i < nppList.Count; i++)
            {
                if (nppList[i].TenNhanPhanPhoi == strNhaPhanPhoi)
                    return nppList[i];
            }
            return null;
        }

        private LoaiSanPham GetLoaiSanPham(String strLoaiSanPham)
        {
            List<LoaiSanPham> lspList = productController.GetLoaiSanPhamList();
            for (int i = 0; i < lspList.Count; i++)
            {
                if (lspList[i].TenLoaiSanPham == strLoaiSanPham)
                {
                    return lspList[i];
                }
            }
            return null;
        }

        protected void btnCapNhapSanPham_Click(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(txtMaSanPham.Text);
            ProductInfo productInfo = new ProductInfo();
            productInfo.ID = id;
            productInfo.TenSanPham = txtTenSanPham.Text;
            productInfo.NSX = GetNSX(dlNhaSanXuat.Text);
            productInfo.MoTa = txtMoTa.Text;
            
            try
            {
                productInfo.Gia = Convert.ToInt32(txtGia.Text);
            }
            catch
            {
                WebMsgBox.Show("Giá không chính xác");
                return;
            }

            if (dlKhuyenMai.Text == "Không có")
                productInfo.Khuyen_Mai = null;
            else
                productInfo.Khuyen_Mai = GetKhuyenMai(dlKhuyenMai.Text);
            productInfo.Nha_Phan_Phoi = GetNhaPhanPhoi(dlNhaPhanPhoi.Text);
            
            try
            {
                productInfo.Diem = Convert.ToInt32(txtDiem.Text);
            }
            catch
            {
                WebMsgBox.Show("Điểm không chính xác");
                return;
            }

            try
            {
                productInfo.SoLuongTon = Convert.ToInt32(txtSoLuong.Text);
            }
            catch
            {
                WebMsgBox.Show("Số lượng không chính xác");
                return;
            }

            try
            {
                productInfo.NgayThemSP = Convert.ToDateTime(txtNgayThemSanPham.Text);
            }
            catch
            {
                WebMsgBox.Show("Ngày thêm sản phẩm không chính xác!");
                return;
            }

            productInfo.BaoHanh = txtBaoHanh.Text;
            productInfo.Loai_San_Pham = GetLoaiSanPham(dlLoaiSanPham.Text);

            switch (dlLoaiSuDung.Text)
            {
                case "Desktop":
                    productInfo.LoaiLinhKien = 0;
                    break;
                case "Laptop":
                    productInfo.LoaiLinhKien = 1;
                    break;
                case "Desktop và Laptop":
                    productInfo.LoaiLinhKien = 2;
                    break;
            }

            if (productController.UpdateProduct(productInfo))
            {
                WebMsgBox.Show("Đã cập nhập sản phẩm!");
                ClientScript.RegisterStartupScript(this.GetType(), "Delay then redirect", "<script type=text/javascript>delayRedirect('/cp/employee/manage/ProductManage.aspx');</script>");
            }
            else
                WebMsgBox.Show("Thất bại khi cập nhập sản phẩm!");
        }
    }
}
